home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / stripasm.arc / STRIPASM.C next >
Text File  |  1987-03-02  |  5KB  |  185 lines

  1. /*        Strip all comments and condense multiple white space charcters    */
  2. /*    from an ASM source                                                                */
  3.  
  4. /*        This program is designed to reduce the number of characters        */
  5. /*    which MASM loads when it INCLUDES a tested file.  It tries to        */
  6. /*    retain the line numbering of the original file.  It anticipates    */
  7. /*    that you will save a commented version and a stripped version.        */
  8. /*    If you discover errors, change the commented version and run this    */
  9. /*    program on it again.                                                                */
  10.  
  11. /*    Lew Paper                                                                            */
  12. /* 3/1/87                                                                                */
  13.  
  14. #include <stdio.h>
  15. #include <dos.h>
  16. #include <string.h>
  17.  
  18. FILE *istream, *ostream;
  19.  
  20. #define    TAB        0x09
  21. #define    RETURN    0x0d
  22. #define    NEWLINE    0x0a
  23. #define    SPACE        0x20
  24. #define FALSE 0
  25. #define TRUE 1
  26.  
  27. #define MAXLINE    512
  28.  
  29. #define ISBLANK(PTR)    (*(PTR) == SPACE || *(PTR) == TAB)
  30. char close_error[] = "Error in closing file %s\n";
  31.  
  32. extern int yesorno(char *);
  33.  
  34. main (argc, argv)
  35. int argc;
  36. char *argv[];
  37. {
  38.     int    maxchar;
  39.     static int    terminator = 0;
  40.     char *p1, *p2;
  41.     static char    line_in[MAXLINE] = {'A'};
  42.                                                     /* the input buffer */
  43.     char    line_out[MAXLINE];                /* the output buffer */
  44.     int    bypass_strip;
  45.  
  46.     if (argc != 3) {
  47.         printf ("\nUsage: %s <input_file> <output_file>\n", argv[0]);
  48.         exit(1);
  49.     }
  50.                     /* try to open input file */
  51.  
  52.     if ((istream = freopen (argv[1], "rb", stdin)) == NULL) {
  53.         printf ("\nCannot open input file %s", argv[1]);
  54.         exit(2);
  55.     }
  56.                     /* check whether to overwrite old output file */
  57.  
  58.     if (ostream = fopen (argv[2], "rb")) {
  59.         fclose (ostream);
  60.         printf ("\n%s already exists.\n", argv[2]);
  61.         if (!yesorno("Do you want to overwrite it (Y or N)?")) {
  62.             close_in (argv[1]);
  63.             exit(3);
  64.             }
  65.         }
  66.     
  67.                     /* try to open output file */
  68.     if ((ostream = fopen (argv[2], "wb")) == NULL) {
  69.         printf ("Cannot open output file %s", argv[2]);
  70.         close_in (argv[1]);
  71.         exit (4);
  72.     }
  73.     
  74.     while ((fgets (line_in, MAXLINE, istream)) != (char *)NULL) {
  75.  
  76.         maxchar = strlen(line_in) - 1;
  77.  
  78.                     /* Comment processing */
  79.  
  80.     if (terminator) {
  81.         if (strchr(line_in, (char) terminator))
  82.                                                                 /* End comment mode the */
  83.                                                                 /* after terminator appears */
  84.             terminator = 0;
  85.         bypass_strip = 1;                                    /* Only output cr, lf */
  86.         line_out[0] = RETURN;
  87.         line_out[1] = NEWLINE;
  88.         line_out[2] = 0;
  89.     }
  90.     else {                                                    /* Check for "comment" */
  91.         for (p1 = &line_in[0]; *p1 && ISBLANK(p1); ++p1);
  92.                                                                 /* Bypass spaces */
  93.         if (!strnicmp(p1, "comment", 7) && ISBLANK((p1 + 7))) {
  94.                                                                 /* Check for "comment " */
  95.             for (p1 += 8; *p1 && ISBLANK(p1); ++p1);
  96.                                                                 /* Bypass spaces */
  97.             terminator = *p1;                                /* Terminator is case */
  98.                                                                 /* insensitive */
  99.             if (terminator)                                /* terminator exists */
  100.                  if (strchr(++p1, (char) terminator))
  101.                                                                 /* is it on the same line? */
  102.                     terminator = 0;                        /* Yes */
  103.             bypass_strip = 1;                                /* Only output cr, lf */
  104.             line_out[0] = RETURN;
  105.             line_out[1] = NEWLINE;
  106.             line_out[2] = 0;
  107.         }
  108.         else
  109.             bypass_strip = 0;                                /* Strip the line */
  110.     }
  111.                     /* Stripping */
  112.         if (!bypass_strip) {
  113.             p1 = &line_in[0];                        /* p1 points at the next */
  114.                                                         /* character to read         */
  115.             p2 = &line_out[0];                    /* p2 points at the next */
  116.                                                         /* character to write     */
  117.             while (*p1) {
  118.                 if (*p1 == ';') {                    /* Strip rest of line for comment */
  119.                     *p1 = 0;                            /* Terminate line_in here */
  120.                     if (p1 > &line_in[0] && ISBLANK((p1-1)))
  121.                                                     /* Can only be one space before */
  122.                                                     /* here, because the program    */
  123.                                                     /* strips repeating spaces and */
  124.                                                     /* tabs */
  125.                         p2--;
  126.                     *p2++ = '\r';
  127.                     *p2++ = '\n';
  128.                 }
  129.                 else if (ISBLANK(p1)) {        /* Strip repeating spaces and tabs */
  130.                     *p2 = *p1;
  131.                     for (++p1, ++p2; *p1 && ISBLANK(p1); ++p1);
  132.                 }
  133.                 else                                    /* Copy character */
  134.                     *p2++ = *p1++;
  135.             }
  136.             *p2 = 0;                                    /* End output line */
  137.  
  138.         }
  139.  
  140.                     /* Output line */
  141.         if (fputs (line_out, ostream)) {
  142.             printf ("Error writing file %s\n", argv[2]);
  143.             printf ("This is usually a full output disk\n");
  144.             (void) close_both (argv[1], argv[2]);
  145.             exit (5);
  146.         }
  147.     }
  148.  
  149.     if (close_both (argv[1], argv[2]))
  150.         exit(6);
  151.     printf ("Stripped file %s to file %s\n", argv[1], argv[2]);
  152.     exit (0);
  153. }
  154.  
  155. output_error (in_name, out_name)
  156.     char *in_name, *out_name;
  157.  
  158. {
  159.     printf ("Error writing file %s\n", out_name);
  160.     printf ("This is usually a full output disk\n");
  161.     (void) close_both (in_name, out_name);
  162.     exit (5);
  163. }
  164.  
  165. close_in (in_name)
  166.     char *in_name;
  167.  
  168. {
  169.     if (fclose (istream))
  170.         printf (close_error, in_name);
  171. }
  172.  
  173. int close_both (in_name, out_name)
  174.     char *in_name, *out_name;
  175.  
  176. {
  177.     int ret_i, ret_o;
  178.  
  179.     if (ret_i = fclose (istream))
  180.         printf (close_error, in_name);
  181.     if (ret_o = fclose (ostream))
  182.         printf (close_error, out_name);
  183.     return (ret_i || ret_o);
  184. }
  185.